package com.qckj.amz.controller;

import com.qckj.amz.domain.ProductImageCategory;
import com.qckj.amz.service.IProductImageCategoryService;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 产品图片分类Controller
 *
 * @author luchuan
 * @since 1.0 2022-03-16
 */
@RestController
@RequestMapping("/amz/productImageCategory")
public class ProductImageCategoryController extends BaseController {
    @Autowired
    private IProductImageCategoryService productImageCategoryService;

    /**
     * 查询分类表信息
     * @param id Integer
     */
    @PreAuthorize("@ss.hasPermi('amz:productImageCategory:edit')")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public AjaxResult info(@RequestParam(value = "id") Long id) {
        try {
            ProductImageCategory productImageCategory = productImageCategoryService.findById(id);
            return AjaxResult.success(productImageCategory);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取产品图片分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productImageCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            ProductImageCategory productImageCategory = productImageCategoryService.findById(id);
            return AjaxResult.success(productImageCategory);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增产品图片分类
     */
    @PreAuthorize("@ss.hasPermi('amz:productImageCategory:add')")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResult add(@RequestBody ProductImageCategory productImageCategory) {
        try {
            AjaxResult res = productImageCategoryService.save(productImageCategory);
            return res;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改产品图片分类
     * @param productImageCategory 修改参数
     */
    @PreAuthorize("@ss.hasPermi('amz:productImageCategory:edit')")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult update(@RequestBody ProductImageCategory productImageCategory) {
        try {
            AjaxResult res = productImageCategoryService.update(productImageCategory);
            return res;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取图片分类列表
     * @param type      类型ID | 类型：1 产品分类，2 附件分类，9 其他
     * @param status    -1=全部，0=未生效，1=已生效
     * @param name      模糊搜索
     * @return
     */
    @RequestMapping(value = "/getListTreeCategroy", method = RequestMethod.GET)
    public AjaxResult getListTreeCategroy(@RequestParam(name = "type") Integer type,
                                  @RequestParam(name = "status") Integer status,
                                  @RequestParam(name = "name", required = false) String name) {
        try {
            AjaxResult res = productImageCategoryService.getListTreeCategory(type,status,name);
            return res;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除分类
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-16
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public AjaxResult delete(@RequestParam(value = "id") Long id) {
        try {
            productImageCategoryService.deleteProductImageCategoryById(id);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }


}
